package com.example.test;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewholder> {
    List<String> mList;
    OnItemClickListener listener;
//    Context context;
    public RecyclerViewAdapter(List<String> list, OnItemClickListener listener) {
        this.mList = list;
        this.listener = listener;
    }


    @Override
    @NonNull
    public MyViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new MyViewholder(view, listener);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewholder holder, int position) {
        holder.tvNum.setText(position + 1 + " ");
        holder.tvContent.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class  MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView tvNum, tvContent;
        LinearLayout rootItem;
        OnItemClickListener listener;
        public MyViewholder(@NonNull View itemView, OnItemClickListener listener) {
            super(itemView);
            tvNum = itemView.findViewById((R.id.tv_num));
            tvContent = itemView.findViewById((R.id.tv_content));
            rootItem = itemView.findViewById(R.id.root_item);
            this.listener = listener;
            rootItem.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClick(getAdapterPosition());
            }
        }
    }
}
